/**
 * Created by Administrator on 2016/1/31.
 */
(function($){
    $.handler = {
        /**
         * 成功状态
         */
        successStatus             : 1,
        /**
         * 失败状态
         */
        errorStatus                : 0,
        /**
         * 错误类型
         */
        errorType                  : {
            timeout : "timeout",	//登录超时
            error : "error" 		//异常
        },
        tpl:[
            '<form class="form-horizontal" id="form-horizontal" role="form">',
            '    <div class="form-group">',
            '        <label class="col-sm-3 control-label no-padding-right" for="form-field-1">用户名：</label>',
            '        <div class="col-sm-9">',
            '            <input type="text" class="form-control" placeholder="请输入用户名" name="loginId" id="loginId" />',
            '        </div>',
            '    </div>',
            '    <div class="form-group">',
            '        <label class="col-sm-3 control-label no-padding-right" for="form-field-1">密码：</label>',
            '        <div class="col-sm-9">',
            '            <input type="password" class="form-control" placeholder="请输入密码" name="password" id="password" />',
            '        </div>',
            '    </div>',
            '    <div class="form-group">',
            '        <label class="col-sm-3 control-label no-padding-right" for="form-field-1">验证码：</label>',
            '        <div class="col-sm-3">',
            '            <input type="text" class="form-control" placeholder="请输入验证码" name="kaptcha" id="kaptcha" />',
            '        </div>',
            '        <div class="col-sm-3">',
            '           <img class="kaptcha form-control" src="'+ $.router.kaptcha+'"/>',
            '        </div>',
            '    </div>',
            '    <div class="form-group">',
            '        <span id="dialog-message-span"></span>',
            '    </div>',
            '</form>'
        ].join(""),
        /**
         * 检查返回值状态
         * @param json
         */
        checkResult:function(result){
        	var me = this;
        	var json;
        	if(!$.isPlainObject(result)){
        		try{
        			json = $.parseJSON(result);
        		}catch(e){
        			json = result;
        		}
        		
        	}else{
        		json = result;
        	}
        	if(typeof(json.success) !== "undefined" || typeof(json.code) !== "undefined"){
                switch(json.type){
                    case me.errorType.timeout:
                        me.showLoginWindow(json.message);
                        return false;
                    case me.errorType.error:
                        me.error(json.message);
                        return false;
                    default :
                        return true;
                }
            }else{
            	return true;
            }
        },
        /**
         * json数据回调
         */
        jsonCallback : function(json,callback){
            var me =this;
            if(me.checkResult(json)){
            	var callbacks = $.Callbacks( "once" );
                callbacks.add(callback);
                callbacks.fire(json);
            }
        },
        htmlCallback : function(html,callback,selector){
            var me =this;
            if(me.checkResult(html)){
            	var $dom = $(selector);
            	$dom.html(html);
            	var callbacks = $.Callbacks( "once" );
                callbacks.add(callback);
                callbacks.fire($dom );
            }
        },
        showLoginWindow : function(message){
            var me = this;
            bootbox.alert({
                size: 'small',
                message: message,

                callback: function(){
                    bootbox.dialog({
                        title:"系统登录",
                        message: me.tpl,
                        closeButton:false,
                        buttons:
                        {
                            "success" :
                            {
                                "label" : "<i class='ace-icon fa fa-check'></i> 登录!",
                                "className" : "btn-sm btn-success",
                                "callback": function() {
                                    //clicked, do something
                                	var form = $("#form-horizontal");
                                	if (!form.valid()) return false;
                            		$.request($.router.login,{
                            			type:"post",
                            			data:form.serializeArray(),
                            			callback:function(json){
                            				if(json.success){
                            					$.loadSideBar();
                            					return true;
                            				}else{
                            					$("#dialog-message-span").html(json.message);
                            					return false;
                            				}
                            			}
                            		});
                                }
                            }
                        }
                    });
                }
            });
        },
        error : function(jqXHR, textStatus, errorThrown){
            console.log("error");
            bootbox.alert("<span class='alert-danger'>"+jqXHR+"</span>");
        }
    }
})(jQuery);
